<title>登录</title>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="css/common.css">
<link rel="stylesheet" href="css/login.css">
<div class="nav">
  新酱爱玩的五子棋
</div>

<div class="login-container">
    <div class="login-dialog">
        <!-- 标题 -->
        <h3>登录-春日部</h3>
        <!-- 输入用户名 -->
        <div class="row">
            <span>用户名</span>
            <input type="text" id="username" name="username">
        </div>
        <!-- 输入密码 -->
        <div class="row">
            <span>密码</span>
            <input type="password" id="password" name="password">
        </div>
        <!-- 提交按钮 -->
        <div class="row submit-row">
            <button id="submit">西红柿炒鸡蛋</button> 
        </div>
    </div>  
    <script src="http://lib.sinaapp.com/js/jquery/1.9.1/jquery-1.9.1.min.js"></script>
<script>
    // 通过 ajax 的方式实现登录过程
    let submitButton = document.querySelector('#submit');
    submitButton.onclick = function() {
        // 1. 先获取到用户名和密码
        let username = document.querySelector('#username').value;
        let password = document.querySelector('#password').value;

        $.ajax({
            method: 'post',
            url: '/login',
            data: {
                username: username,
                password: password
            },
            success: function(data) {
                console.log(JSON.stringify(data));
                if (data && data.userId > 0) {
                    // 登录成功, 跳转到游戏大厅
                    alert("登录成功!")
                    location.assign('/game_hall.html');
                } else {
                    alert("登录失败! 用户名密码错误! 或者该账号正在游戏中!");
                }
            }
        });
    }
</script>
</div>